//
//  ITDownLoadViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/18.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITDownLoadViewController.h"
#import "DownloadService.h"

@interface ITDownLoadViewController ()

@property (nonatomic, strong) UILabel *progressLabel;

@property (nonatomic, copy) Checkpoint *checkpoint;
@property (nonatomic, strong) DownloadRequest *downloadRequest;
@property (nonatomic, strong) DownloadService *downloadService;

@end

@implementation ITDownLoadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupViews];
    [self initDownLoadTask];
}

-(void)setupViews {
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [startBtn setTitle:@"开始" forState:UIControlStateNormal];
    startBtn.frame = CGRectMake(50, 200, 100, 44);
    [self.view addSubview:startBtn];
    [startBtn addTarget:self action:@selector(startAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *pauseBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [pauseBtn setTitle:@"暂停" forState:UIControlStateNormal];
    pauseBtn.frame = CGRectMake(50, 250, 100, 44);
    [self.view addSubview:pauseBtn];
    [pauseBtn addTarget:self action:@selector(pauseAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.frame = CGRectMake(50, 300, 100, 44);
    [self.view addSubview:cancelBtn];
    [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.progressLabel = [[UILabel alloc] init];
    self.progressLabel.text = @"0";
    [self.view addSubview:self.progressLabel];
    self.progressLabel.frame = CGRectMake(200, 300, 100, 44);
    self.progressLabel.textColor = [UIColor blueColor];
    
}
-(void)initDownLoadTask {
    _downloadRequest = [DownloadRequest new];
    _downloadRequest.sourceURLString = @"http://pic1.sc.chinaz.com/files/pic/pic9/202009/apic27891.jpg";       // 设置资源的url
    _downloadRequest.headURLString = @"http://pic1.sc.chinaz.com/files/pic/pic9/202009/apic27891.jpg";
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    _downloadRequest.downloadFilePath = [documentPath stringByAppendingPathComponent:@"apic27891.jpg"];   //设置下载文件的本地保存路径
    NSLog(@"存放地址 %@", documentPath);
    __weak typeof(self) wSelf = self;
    _downloadRequest.downloadProgress = ^(int64_t bytesReceived, int64_t totalBytesReceived, int64_t totalBytesExpectToReceived) {
        // totalBytesReceived是当前客户端已经缓存了的字节数,totalBytesExpectToReceived是总共需要下载的字节数。
        dispatch_async(dispatch_get_main_queue(), ^{
            __strong typeof(self) sSelf = wSelf;
            CGFloat fProgress = totalBytesReceived * 1.f / totalBytesExpectToReceived;
            NSLog(@"下载进度 %.2f",fProgress * 100);
            sSelf.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", fProgress * 100];
            
        });
    };
    _downloadRequest.failure = ^(NSError *error) {
        __strong typeof(self) sSelf = wSelf;
        sSelf.checkpoint = error.userInfo[@"checkpoint"];
        dispatch_async(dispatch_get_main_queue(), ^{
            sSelf.progressLabel.text = @"下载失败";
        });
    };
    _downloadRequest.success = ^(NSDictionary *result) {
        __strong typeof(self) sSelf = wSelf;
        sSelf.progressLabel.text = @"下载成功";
    };
    _downloadRequest.checkpoint = self.checkpoint;
    
    _downloadService = [DownloadService downloadServiceWithRequest:_downloadRequest];
}
-(void)startAction{
    [_downloadService resume];
        
}

-(void)pauseAction{
    [_downloadService pause];
    
}

-(void)cancelAction{
    [_downloadService cancel];
}

@end
